// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join('');
}

//将小写字母转成大写字母
function upperCase(str) {
  let arr = str.split("");
  let newStr = "";
  // 通过数组的forEach方法来遍历数组
  arr.forEach(function (value) {
    if (value >= 'a' && value <= 'z')
      newStr += value.toUpperCase();
    else
      newStr += value;
  });
  return newStr;
}

//字符串转换成16进制
function concatenation(str) {
  var val = "";
  var rule = /.*[\u4e00-\u9fa5]+.*$/
  for (var i = 0; i < str.length; i++) {
    if (val == "" && rule.test(str[i]) === false) {
      val = str.charCodeAt(i).toString(16);
    } else if (val == "" && rule.test(str[i])) {
      val = encodeURI(str[i]).replace(/%/gm, '')
    } else if (rule.test(str[i]) === false) {
      // val += "," + str.charCodeAt(i).toString(16);
      val += str.charCodeAt(i).toString(16);
    } else if (rule.test(str[i])) {
      // val += "," + encodeURI(str[i]).replace(/%/gm, '')
      val += encodeURI(str[i]).replace(/%/gm, '')
    }
  }
  // console.log(val, 'WIFI信息')
  return val;
}

//16进制转换成字符串
function hexToString(hex) {
  var arr = hex.split("")
  var out = ""
  for (var i = 0; i < arr.length / 2; i++) {
    var tmp = "0x" + arr[i * 2] + arr[i * 2 + 1]
    var charValue = String.fromCharCode(tmp);
    out += charValue
  }
  return out
};

function showModal(msg) {
  uni.showModal({
    title: '提示',
    showCancel: false,
    confirmText: "知道了",
    confirmColor: "#0080FF",
    content: msg
  })
}
//WiFi错误提示
function wifitips(val) {
  let tips = ""
  switch (val) {
    case 12000:
      tips = "未先调用 startWifi 接口"
      break;
    case 12001:
      tips = "当前系统不支持相关能力"
      break;
    case 12002:
      tips = "密码错误"
      break;
    case 12003:
      tips = "连接超时"
      break;
    case 12004:
      tips = "重复连接 Wi-Fi"
      break;
    case 12005:
      tips = "未打开 Wi-Fi 开关"
      break;
    case 12006:
      tips = "未打开 GPS 定位开关"
      break;
    case 12007:
      tips = "用户拒绝授权链接 Wi-Fi"
      break;
    case 12008:
      tips = "无效 SSID"
      break;
    case 12009:
      tips = "系统运营商配置拒绝连接 Wi-Fi"
      break;
    case 12010:
      tips = "系统其他错误"
      break;
    case 12011:
      tips = "应用在后台无法配置 Wi-Fi"
      break;
    case 12013:
      tips = "系统保存的 Wi-Fi 配置过期，建议忘记 Wi-Fi 后重试"
      break;
    default:
      break;
  }
  return tips
}

//object.fail 回调函数返回的 state 参数（仅 iOS）
function openbluetoothtips(val) {
  let tips = ""
  switch (val) {
    case 0:
      tips = "未知"
      break;
    case 1:
      tips = "重置中"
      break;
    case 2:
      tips = "不支持"
      break;
    case 3:
      tips = "未授权"
      break;
    case 4:
      tips = "未开启"
      break;
  }
  return tips
}

//蓝牙错误提示信息
function bluetoothtips(val) {
  let tips = ""
  switch (val) {
    case -1:
      tips = "已连接"
      break;
    case 10000:
      tips = "未初始化蓝牙适配器"
      break;
    case 10001:
      tips = "当前蓝牙适配器不可用"
      break;
    case 10002:
      tips = "没有找到指定设备"
      break;
    case 10003:
      tips = "连接失败"
      break;
    case 10004:
      tips = "没有找到指定服务"
      break;
    case 10005:
      tips = "没有找到指定特征"
      break;
    case 10006:
      tips = "当前连接已断开"
      break;
    case 10007:
      tips = "当前特征不支持此操作"
      break;
    case 10008:
      tips = "其余所有系统上报的异常"
      break;
    case 10009:
      tips = "Android 系统特有，系统版本低于 4.3 不支持 BLE"
      break;
    case 10012:
      tips = "连接超时"
      break;
    case 10013:
      tips = "连接 deviceId 为空或者是格式不正确"
      break;
    default:
      tips = "目前蓝牙调试功能暂不支持 Mac 以外的平台"
      break;
  }
  return tips
}

//停止搜寻附近的蓝牙外围设备
function stopBluetoothDevicesDiscovery(callBack) {
  uni.stopBluetoothDevicesDiscovery({
    success: res => {
      callBack(res)
      console.log(res, '停止搜索设备')
    },
    fail: error => {
      callBack(error)
      console.log(error, '停止搜索设备')
    }
  })
};
//蓝牙模块初始化
function openBluetoothAdapter(callBack) {
  uni.openBluetoothAdapter({
    success: res => {
      callBack(res)
      console.log(res, '初始化蓝牙模块')
    },
    fail: error => {
      callBack(error)
      console.log(error, '初始化蓝牙模块')
    }
  })
};
//关闭蓝牙模块。
function closeBluetoothAdapter(callBack) {
  uni.closeBluetoothAdapter({
    success: res => {
      callBack(res)
      console.log(res, '关闭蓝牙模块')
    },
    fail: error => {
      callBack(error)
      console.log(error, '关闭蓝牙模块')
    }
  })
};
//获取本机蓝牙适配器状态
function getBluetoothAdapterState(callBack) {
  uni.getBluetoothAdapterState({
    success: res => {
      callBack(res)
      console.log(res, '获取本机蓝牙适配器状态')
    },
    fail: error => {
      callBack(error)
      console.log(error, '获取本机蓝牙适配器状态')
    }
  })
};
//搜索蓝牙外围设备
function startBluetoothDevicesDiscovery(callBack, services = []) {
  uni.startBluetoothDevicesDiscovery({
    services: services,
    success: res => {
      callBack(res)
      console.log(res, '搜索蓝牙外围设备')
    },
    fail: error => {
      callBack(error)
      console.log(error, '搜索蓝牙外围设备')
    },
  })
}
// 监听寻找周围新的蓝牙设备
function onBluetoothDeviceFound(callBack) {
  uni.onBluetoothDeviceFound(function (res) {
    callBack(res)
    //  console.log(res,'监听寻找周围新的蓝牙设备')
  })
}


module.exports = {
  ab2hex,
  upperCase,
  concatenation,
  hexToString,
  showModal,
  wifitips,
  openbluetoothtips,
  bluetoothtips,
  stopBluetoothDevicesDiscovery,
  openBluetoothAdapter,
  closeBluetoothAdapter,
  getBluetoothAdapterState,
  startBluetoothDevicesDiscovery,
  onBluetoothDeviceFound
}